var faultTree;
$(function () {
    $("#jqGrid").Grid({
        url: '../devtype/list',
        rownumWidth: 40,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', align: 'center', key: true, hidden: true},
            {label: '类型编码', name: 'typeCode', index: 'TYPE_CODE', width: 80, align: 'center'},
            {label: '类型名称', name: 'typeName', index: 'TYPE_NAME', width: 80, align: 'center'},
            {
                label: '上级类型', name: 'parentName', index: 'TYPE_PID', align: 'center', width: 100,
                formatter: function (value) {
                    return value === null ? '无' : value;
                }
            },
            {
                label: '类型标志', name: 'typeTp', index: 'TYPE_TP', width: 80, align: 'center',
                formatter: function (value) {
                    return sysEnums.DEV_TYPE_SPEC[value];
                }
            },
            {label: '备注', name: 'remark', index: 'REMARK', width: 80, align: 'center'},
            /*{label: '添加人员', name: 'createUser', index: 'CREATE_USER', width: 80, align: 'center'},
            {
                label: '添加时间', name: 'createDate', index: 'CREATE_DATE', width: 80, align: 'center',
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd hh:mm");
                }
            },*/
            // {label: '删除标识', name: 'delFlag', index: 'DEL_FLAG', width: 80,align: 'center'},

        ]
    });
    vm.getDevTypeTree();
});
// 设备类别树配置
var devTypeTreeSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "typeCode",
            pIdKey: "typePid",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: function(e, treeObj, treeNode) {
            vm.q.typePid = treeObj.typeCode;
            vm.query();
        }
    }
};

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devType: {id:''},
        showDevTP: enumsToList("DEV_TYPE_SPEC"),
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '', // 类别名称
            code: '', // 类别编码
            typePid:'' // 父类别编码
        }
    },
    methods: {
        getDevTypeTree: function(){
            //加载故障类型树
            Ajax.request({
                url: "../devtype/listAll",
                async: true,
                successCallback: function (r) {
                    var tree = r.list;
                    faultTree = $.fn.zTree.init($("#devTypeTree"), devTypeTreeSetting, tree);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devType.id = '';
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.devType.id == (null || '') ? "../devtype/save" : "../devtype/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devType),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devtype/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../devtype/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.devType = r.devType;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                search: true,
                postData: vm.q,
                page: page
            }).trigger("reloadGrid", [{ page: 1}]);
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '', // 类别名称
                code: '', // 类别编码
                typePid:'' // 父类别编码
            };
            // 取消选中
            faultTree.cancelSelectedNode();
            // 收缩所有已展开项
            faultTree.expandAll(false);
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});